package com.qk.management.aop;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.qk.vo.portal.PortalVO;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @Author:Shuna
 * @Date: 2025/10/17 12:16
 * @Description:
 */
@Aspect
@Slf4j
@Component
public class PortalCacheAspect {

    @Autowired
    private StringRedisTemplate redisClient;
    private static final String CACHE_PORTAL_KEY_PREFIX = "qk:portal:";



    @Pointcut(value = "@annotation(com.qk.management.aop.anno.PortalCache)")
    public void cache() {

    }

    @Around("cache()")
    public Object cacheTimeCount(ProceedingJoinPoint pjp) throws Throwable {
        String redisKey = CACHE_PORTAL_KEY_PREFIX + "reportOverview";
        String jsonStr = null;
        try {
            jsonStr = redisClient.opsForValue().get(redisKey);
            if (ObjectUtil.isNotEmpty(jsonStr)) {
                PortalVO vo = JSONUtil.toBean(jsonStr, PortalVO.class);
                return vo;
            }
        } catch (Exception e) {
            log.error("查询缓存异常", e);
        }
        Object proceed = pjp.proceed();
        try {
            String json = JSONUtil.toJsonStr(proceed);
            redisClient.opsForValue().set(redisKey, json);
        } catch (Exception e) {
            log.error("添加缓存异常", e);
        }
        return proceed;
    }
}
